home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6794 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  54 lines

  1. Newsgroups: comp.lang.c++
  2. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!bbnews1!trsvr!news
  3. From: Benjamin Romer <bmr1@trpo4.tr.unisys.com>
  4. Subject: Re: [Q] What is an "exception"?
  5. Sender: news@tr.unisys.com (cnews news id.)
  6. Message-ID: <3128C17B.7A6E@trpo4.tr.unisys.com>
  7. Date: Mon, 19 Feb 1996 18:29:15 GMT
  8. X-Nntp-Posting-Host: bmr1.tr.unisys.com
  9. Content-Transfer-Encoding: 7bit
  10. Content-Type: text/plain; charset=us-ascii
  11. References: <4g8l4i$1m9@mtinsc01-mgt.ops.worldnet.att.net>
  12. Mime-Version: 1.0
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14. Organization: Unisys Corp.
  15.  
  16. Jeff Nikodym wrote:
  17. > Im learing MSVC++2, and the book at one point states that one of the
  18. > functions within a certain class will "throw an exception if it fails,
  19. > so be ready to catch it". Can someone explain this?
  20.  
  21. The ANSI C++ Standard has introduced a new error-catching method
  22. to the C++ language called exceptions. Basically, a procedure can throw
  23. an "exception object", representing an error, and the calling function
  24. can catch that error and handle it without crashing.
  25.  
  26. Here's a short code snippet:
  27. doFunctionThatThrowsException();
  28. {
  29.     throw "Character String Object Exception";
  30.     //keyword throw causes an exception object of the type following
  31.     //the keyword to be thrown.
  32. }
  33.  
  34. try        //keyword try indicates code within brackets can cause 
  35. {        //an exception.
  36.     doFunctionThatThrowsException();    //function can throw
  37. }                        //an exception
  38. catch(char* msg) //keyword catch contains code to handle exception;
  39. {
  40.     cout << "Error:" << msg << "\n";
  41. }
  42.  
  43. Most newer books on C++ cover this new topic. Check your local bookstore.
  44.  
  45. Hope this helps.
  46.  
  47. Freely Yours,
  48. Benjamin M. Romer
  49. Software Engineer
  50. Unisys Corporation
  51.  
  52. #include <stddisclaim.h>
  53.